using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;
using LockCrypt.Core;
using System;

namespace LockCrypt {
    public class DefaultDragSourceAdvisor : IDragSourceAdvisor {
        public DragDropEffects SupportedEffects {
            get { return DragDropEffects.Move | DragDropEffects.Copy; }
        }

        public UIElement SourceUI { get; set; }

        public DataObject GetDataObject(UIElement draggedElt, Point offsetPoint) {
            INestable draggedItem = GetDraggedItem(draggedElt);
            DataObject data = new DataObject();
            data.SetData(draggedItem.GetType(), draggedItem);
            data.SetData("uielement", draggedElt);
            return data;
        }

        /// <summary>
        /// Determines whether the UI element which was dragged represented an INestable (eg: in a datatemplate).
        /// </summary>
        /// <param name="dragElt">The drag element.</param>
        /// <returns>
        /// 	<c>true</c> if the specified element is draggable; otherwise, <c>false</c>.
        /// </returns>
        public bool IsDraggable(UIElement dragElt) {
            return GetDraggedItem(dragElt) != null;
        }

        /// <summary>
        /// Gets a dragged INestable instance from an element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The INestable item which was dragged.</returns>
        private INestable GetDraggedItem(UIElement element) {
            ListBoxItem parentItem = Lime49.WPF.Utils.GetParentOfType(element, typeof(ListBoxItem)) as ListBoxItem;
            if(parentItem != null)
                return parentItem.Content as INestable;
            return null;
        }

        public void FinishDrag(UIElement draggedElt, DragDropEffects finalEffects) {
            /*if((finalEffects & DragDropEffects.Move) == DragDropEffects.Move) {
                ListBoxItem itm = SourceUI as ListBoxItem;
                var lstBox = ListBox.ItemsControlFromItemContainer(itm) as ListBox;
                if(lstBox != null) {
                    lstBox.Items.Remove(draggedElt);
                }
            }*/
        }
    }
}